package com.example.emos.workflow.bpmn;

import cn.hutool.core.map.MapUtil;
import com.example.emos.workflow.db.dao.TbLeaveDao;
import com.example.emos.workflow.db.dao.TbUserDao;
import com.example.emos.workflow.exception.EmosException;

import com.example.emos.workflow.task.EmailTask;
import com.example.emos.workflow.task.SendEmailTask;
import org.activiti.engine.HistoryService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricTaskInstanceQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Component
public class NotifyLeaveService implements JavaDelegate {
    @Autowired
    private HistoryService historyService;

    @Autowired
    private TbLeaveDao leaveDao;

    @Autowired
    private TbUserDao userDao;

    @Autowired
    private SendEmailTask sendEmailTask;

    @Override
    public void execute(DelegateExecution delegateExecution) {
//        HistoricTaskInstance taskInstance = historyService.createHistoricTaskInstanceQuery().includeTaskLocalVariables()
//                .includeProcessVariables()
//                .processInstanceId(delegateExecution.getProcessInstanceId())
//                .orderByHistoricTaskInstanceEndTime()
//                .orderByTaskCreateTime()
//                .list()
//                .get(0);
        // 创建历史任务查询对象


//        HistoricTaskInstanceQuery historicTaskInstanceQuery1 = historyService.createHistoricTaskInstanceQuery().includeTaskLocalVariables()
//                .includeProcessVariables().processInstanceId(delegateExecution.getProcessInstanceId());
//        HistoricTaskInstanceQuery query = historicTaskInstanceQuery1.orderByHistoricTaskInstanceEndTime()
//                .orderByTaskCreateTime();
//        list = query.list();
//
//        HistoricTaskInstance taskInstance = list.get(0);
//
//        String result = taskInstance.getTaskLocalVariables().get("result").toString();
//        delegateExecution.setVariable("result", result);

        HistoricTaskInstanceQuery taskQuery = historyService.createHistoricTaskInstanceQuery()
                .orderByHistoricTaskInstanceStartTime().desc()
                .includeTaskLocalVariables().includeProcessVariables()
                .processInstanceId(delegateExecution.getProcessInstanceId());

        HistoricTaskInstance taskInstance = taskQuery.list().get(0);
        String result = taskInstance.getTaskLocalVariables().get("result").toString();
        //执行完成之后将可归档由 false 改为true
        delegateExecution.setVariable("result", result);
        delegateExecution.setVariable("filing",true);

        String instanceId = delegateExecution.getProcessInstanceId();
        int status = "同意".equals(result) ? 3 : 2;

        HashMap param = new HashMap() {{
            put("status", status);
            //put("status", 3 );
            put("instanceId", instanceId);
        }};

        int rows = leaveDao.updateLeaveStatus(param);
        if (rows != 1) {
            throw new EmosException("更新请假记录状态失败");
        } else {
            //Map<String, Object> processVariables = taskInstance.getProcessVariables();
            Map<String, Object> map = taskInstance.getTaskLocalVariables();
            HashMap leaveMap = leaveDao.searchLeaveByInstanceId(instanceId);
            String start = MapUtil.getStr(leaveMap, "start");
            String end =MapUtil.getStr(leaveMap,"end");
            map.put("start",start);
            map.put("end",end);
            sendEmailTask.SendEmailToApproval(map);

        }

    }
}
